package com.ubivelox.uienginesample_20100726_002;

import java.io.InputStream;
import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import com.ubivelox.uienginesample_20100726_002.MyLayout.OnUpdateLayout;
import com.ubivelox.uienginesample_20100726_002.PlaneRenderer.TextureLoader;

import android.app.ActivityGroup;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLES10;
import android.opengl.GLSurfaceView;
import android.opengl.GLUtils;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.ViewGroup.LayoutParams;

public class MainActivity extends ActivityGroup implements TextureLoader{
	
	private static final int DIALOG_SELECT_ACTIVITY = 0;	
	
	private View			mView;
	private MyLayout		mLayout;
	private Bitmap			mBitmap;
	private PlaneRenderer	mPlaneRenderer;
	private boolean		mBitmapChanges = false;
	
	private GLSurfaceView mGlSurfaceView;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.main);
        mLayout = (MyLayout) findViewById(R.id.mylayout);
        mLayout.setOnUpdateLayout(new OnUpdateLayout() {
			
			@Override
			public void UpdateLayout(Bitmap bitmap) {
				mBitmap = bitmap;
				mBitmapChanges = true;
			}
		});
        InputStream is = getResources().openRawResource(R.drawable.nehe);
		mBitmap = BitmapFactory.decodeStream(is);
        showDialog(DIALOG_SELECT_ACTIVITY);    
        mGlSurfaceView = new GLSurfaceView(this);
        mPlaneRenderer = new PlaneRenderer(this, this);      
        mGlSurfaceView.setRenderer(mPlaneRenderer);
        addContentView(mGlSurfaceView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    }
    
    @Override
    protected Dialog onCreateDialog(int id) {    	    	    	    	
    	
    	return new AlertDialog.Builder(this)
    	.setTitle("Select Texture...")
    	.setItems(R.array.select_dialog_items, new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				String action = "test.activity.action.GROUP" + String.valueOf(which + 1);
				String category = "test.activity.category.GROUP" + String.valueOf(which + 1);
				Intent i = new Intent(action); 
				i.addCategory(category);
				mView = GetActivity(i);
				if(mView != null)					
					 mLayout.addView(mView);									 
			}
		})
		.create();
    }
    
    private View GetActivity( Intent intent ) {
		List<ResolveInfo> 	resolveInfoList = getPackageManager().queryIntentActivities(intent, 0);
		if( resolveInfoList.size() < 1 ) return null;
		ResolveInfo		resolveInfo = resolveInfoList.get(0);
		ComponentName	componentName = new ComponentName(
				resolveInfo.activityInfo.applicationInfo.packageName,
				resolveInfo.activityInfo.name );
		Intent intent2 = new Intent();
		intent2.setComponent( componentName );
		Window	window = getLocalActivityManager().startActivity("Starting video1 activity", intent2);
		return window.getDecorView();
	}

	@Override
	public void loadTexture(GL10 gl, int[] textures) {
		if(mBitmapChanges == true){		
			gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
			
			gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
	        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

	        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
	        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);
			
			GLUtils.texImage2D(GLES10.GL_TEXTURE_2D, 0, mBitmap, 0);
			mBitmapChanges = false;
		}
	}
}